import toTree from '../methods/transition'
let autoRouters = import.meta.glob('../pages/home/**/*.vue')
//eager方法 可以获取默认导入的组件
let defaults = import.meta.globEager('../pages/home/**/*.vue')
// console.log(autoRouters);
// console.log(defaults);
let routersArr = []
for (let item in autoRouters) {
    let component = autoRouters[item]
    let reg = /^\.{2}\/pages\/home\/(.+)\/index\.vue$/
    let newautoRouters = item.match(reg)
    // console.log(item,newautoRouters);
    if (newautoRouters) {
        let route = {
            path: newautoRouters[1],
            component,
            meta:defaults[item]
        }
        routersArr.push(route)
        // console.log(routersArr);
    }
}

//给扁平数据加上id和pid
routersArr.forEach(routes => {
    // console.log(route);
    // console.log(routersArr);
    let pathArr = routes.path.split('/')
    // console.log(pathArr);
    if (pathArr.length === 1) {
        routes.id = pathArr.join('')
    } else {
        // console.log(pathArr);
        let newPathArr = [...pathArr]
        routes.id = newPathArr.join('/')
        let paths = newPathArr.pop()
        routes.pid = newPathArr.join('/')
        routes.path = paths
    }
})
// console.log(routersArr);
// console.log(routersArr);
// 扁平转树形
let routes = toTree(routersArr, 'id', 'pid')
console.log(routes);
// export default routersArr
export default routes